/*
 * Copyright (C) Francisco Morero Peyrona. All rights reserved.
 *
 * This software is published under the terms of Open Source
 * License version 1.1, a copy of which has been included with this
 * distribution in the License.txt file.
 */

package jdbcmanager.app;

import javax.swing.*;

import jdbcmanager.swing.*;

/**
 * The "User Interface" tab of app config.
 * 
 * @author Francisco Morero Peyrona
 */
final class ConfigTabGui extends JPanel
{
   private JComboBox cmbLAF;
   private JxSpinner spnFontSize;
   private JCheckBox chkUseOpenGL;
   private JCheckBox chkAskOnExit;
   
   //-------------------------------------------------------------------------//
   
   ConfigTabGui()
   {
      init();
   }
   
   void save()
   {
	   // Look and Feel (if user selects OS Native L&F we MUST store "" (see LookAndFeel.setLookAndFeel(...)))  
	   App.props().setLookAndFeel( (this.cmbLAF.getSelectedIndex() == 0) ? "" : (String) this.cmbLAF.getSelectedItem() );
       
	   // Default font size
	   Integer intFontSize = (Integer) this.spnFontSize.getValue();
	   App.props().setFontSize( intFontSize.intValue() );
	   
	   // Use OpenGL
	   App.props().setUseOpenGL( this.chkUseOpenGL.isSelected() );
	   System.setProperty( "sun.java2d.opengl", (this.chkUseOpenGL.isSelected()) ? "true" : "false" );
	   
	   // Confirm on exit
	   App.props().setAskOnExit( this.chkAskOnExit.isSelected() );
   }
   
   private void init()
   {
      // Look and Feel ------------------------------------------
      UIManager.LookAndFeelInfo[] lafInfo = UIManager.getInstalledLookAndFeels();
      String[] asLAF = new String[ lafInfo.length + 1 ];
               asLAF[0] = "OS native";
      
      for( int n = 0; n < lafInfo.length; n++ )
         asLAF[n+1] = lafInfo[n].getName();
      
      String sSelectedLAF = App.props().getLookAndFeel( "" );
      int    nLAF         = (sSelectedLAF.length() == 0) ? 0 : App.indexOf( asLAF, sSelectedLAF, true );
      
      this.cmbLAF = new JComboBox( asLAF );
      this.cmbLAF.setSelectedIndex( Math.max( nLAF, 0 ) );
      
      // Default font size --------------------------------------
      int nCurrentSize = App.props().getFontSize( 11 );
      this.spnFontSize = new JxSpinner( new SpinnerNumberModel( nCurrentSize, 7, 24, 1 ) );
      
      // Use OpenGL to paint ------------------------------------
      String sOpenGL  = System.getProperty( "sun.java2d.opengl" );
             sOpenGL  = (sOpenGL == null) ? "" : sOpenGL.toUpperCase();
      String sJavaVer = System.getProperty( "java.version" );
      
      this.chkUseOpenGL = new JCheckBox( "Use OpenGL acceleration",
                                         App.props().getUseOpenGL( sOpenGL.equals( "TRUE" ) ) );
      this.chkUseOpenGL.setEnabled( ! (sJavaVer.charAt( 0 ) == '1' && sJavaVer.charAt( 2 ) < '5') );
      
      // Confirm on exit --------------------------------------
      this.chkAskOnExit = new JCheckBox( "Confirm on application exit",
                                         App.props().getAskOnExit( true ) );
      
      // Add controls to this panel ----------------------------
      // To use another panel with BorderLayout and place all in NORTH is a trick to avoid using GridBagLayout()      
      JPanel pnl = new JPanel( new java.awt.GridLayout( 4,2,5,8 ) );
             pnl.setBorder( BorderFactory.createEmptyBorder( 9,9,9,9 ) );
      
      pnl.add( new JLabel( "Look and Feel" ) );
      pnl.add( this.cmbLAF );
      
      pnl.add( new JLabel( "Default font size" ) );
      pnl.add( this.spnFontSize ); 
      
      pnl.add( this.chkUseOpenGL );
      pnl.add( new JLabel() );      // Trick to have even number of components
      
      pnl.add( this.chkAskOnExit );
      pnl.add( new JLabel() );      // Trick to have even number of components
      
      setLayout( new java.awt.BorderLayout() );
      add( pnl, java.awt.BorderLayout.NORTH );     
   }
}